package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.AppInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.fragment.MyCollectionFragment;
import com.wude.guild.views.RoundAngleImageView;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的收藏
 * Created by Administrator on 2017/3/20.
 */

public class MyshouHolder extends BaseHolder<AppInfo> {
    @BindView(R.id.home_game_icon)
    RoundAngleImageView homeGameIcon;
    @BindView(R.id.home_game_name)
    TextView homeGameName;
    @BindView(R.id.home_game_ratingBar)
    RatingBar homeGameRatingBar;
    @BindView(R.id.game_size)
    TextView gameSize;
    @BindView(R.id.down_num)
    TextView downNum;
    @BindView(R.id.home_game_size)
    TextView homeGameSize;
    @BindView(R.id.jianjie)
    TextView jianjie;
    @BindView(R.id.quxiao)
    TextView quxiao;
    private AppInfo app;
    private MyCollectionFragment fragment;

    public MyshouHolder(MyCollectionFragment fragment) {
        this.fragment = fragment;
    }

    @Override
    protected void refreshView(AppInfo appInfo, int position, Activity activity) {
        this.app = appInfo;
        MCUtils.fillImage(homeGameIcon, appInfo.iconurl);
        String jieQu = Utils.getJieQu(appInfo.name);
        homeGameName.setText(jieQu);
        homeGameRatingBar.setRating(appInfo.rating);
        gameSize.setText(appInfo.size);
        downNum.setText(appInfo.DownNum + "人下载");
        jianjie.setText(appInfo.features);
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_mycollect, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @OnClick(R.id.quxiao)
    public void onClick() {
        UserInfo loginUser = Utils.getLoginUser();
        if (app != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("token", loginUser.token);
            map.put("game_id", "" + app.id);
            map.put("status", "2");
            HttpCom.POST(handler, HttpCom.getShouCangUrl(), map, false);
        }
    }

    /**
     * 取消收藏
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean a = HttpUtils.DNSShou(msg.obj.toString());
                    if (a) {
                        fragment.initdata(1);
                    } else {
                        ToastUtil.showToast("操作失败");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
